package com.woniuxy.woniuhis.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.woniuxy.woniuhis.entity.AllotItem;
import com.woniuxy.woniuhis.exception.BigException;
import com.woniuxy.woniuhis.exception.MedicineServiceException;
import com.woniuxy.woniuhis.exception.RepeatException;
import com.woniuxy.woniuhis.service.AlloItemService;
import com.woniuxy.woniuhis.service.MedicineService;
import com.woniuxy.woniuhis.util.ResponseInfo;

@Controller
public class AllotController {

	@Autowired
	AlloItemService allotService;

	@Autowired
	MedicineService medicineService;

	/**
	 * @param req
	 * @return 根据药物名字查调拨历史IDEcontroller方法
	 */
	@RequestMapping("getHis")
	@ResponseBody
	public ResponseInfo findAllotController(String mediName) {
		AllotItem allotItem = new AllotItem();
		allotItem.setMediName(mediName);
		try {
			List<AllotItem> his = allotService.findAllotByMediName(allotItem);

			ResponseInfo responseInfo = new ResponseInfo();
			responseInfo.setData(his);
			responseInfo.setMsg("查询历史");
			responseInfo.setState(200);

			return responseInfo;
		} catch (MedicineServiceException e) {
			e.printStackTrace();
			ResponseInfo responseInfo = new ResponseInfo();
			responseInfo.setMsg("findAllotController服务方法错误");
			responseInfo.setState(500);
			return responseInfo;
		} catch (Exception e) {
			e.printStackTrace();
			ResponseInfo responseInfo = new ResponseInfo();
			responseInfo.setMsg("其他错误");
			responseInfo.setState(500);
			return responseInfo;
		}
	}

	/**
	 * @param req
	 * @return 修改库存的controller方法
	 */
	@RequestMapping("allotMedicine")
	@ResponseBody
	public ResponseInfo allotMediController(HttpServletRequest req) {
		// 从前端输入 @药物id @药物名字 @当前仓库 @目标仓库 调拨数量 //经办人

		System.out.println("allotMediController");

		String mediId = req.getParameter("mediId");
		System.out.println(mediId);
		String mediName = req.getParameter("mediName");
		System.out.println(mediName);
		String pharId = req.getParameter("pharId");
		System.out.println(pharId);
		String target = req.getParameter("target");
		System.out.println(target);
		String amount = req.getParameter("amount");
		System.out.println(amount);
		String stock = req.getParameter("stock");
		System.out.println(stock);

		try {

			AllotItem allotItem = new AllotItem();

			// 防止自己掉自己
			if (pharId.equals(target)) {
				throw new RepeatException();
			}
			// 不能调拨大于库存
			if (Integer.parseInt(amount) > Integer.parseInt(stock)) {
				throw new BigException();
			}

			if (mediId != null && mediId != "") {
				allotItem.setMediId(mediId);
			} else {
				throw new NullPointerException();
			}
			if (mediName != null && mediName != "") {
				allotItem.setMediName(mediName);
			} else {
				throw new NullPointerException();
			}
			if (pharId != null && pharId != "") {
				allotItem.setOutputId(pharId);
			} else {
				throw new NullPointerException();
			}
			if (target != null && target != "") {
				allotItem.setInputId(target);
			} else {
				throw new NullPointerException();
			}

			if (amount != null) {
				allotItem.setAmount(Integer.parseInt(amount));
			} else {
				throw new NullPointerException();
			}

			allotService.modifyAllotItem(allotItem);

			ResponseInfo responseInfo = new ResponseInfo();
			responseInfo.setMsg("修改药品成功");
			responseInfo.setState(200);

			return responseInfo;
		} catch (BigException e) {
			System.out.println("调拨数量不能大于仓储数量");
			e.printStackTrace();

			ResponseInfo responseInfo = new ResponseInfo();
			responseInfo.setMsg("调拨数量不能大于仓储数量");
			responseInfo.setState(500);
			return responseInfo;
		} catch (RepeatException e) {
			System.out.println("不能同号调拨");
			e.printStackTrace();

			ResponseInfo responseInfo = new ResponseInfo();
			responseInfo.setMsg("不能同号调拨");
			responseInfo.setState(500);
			return responseInfo;
		} catch (MedicineServiceException e) {
			System.out.println("allotMediController错误");
			e.printStackTrace();

			ResponseInfo responseInfo = new ResponseInfo();
			responseInfo.setMsg("allotMediController服务方法错误");
			responseInfo.setState(500);
			return responseInfo;
		} catch (NullPointerException e) {
			System.out.println("不完整错误");
			e.printStackTrace();

			ResponseInfo responseInfo = new ResponseInfo();
			responseInfo.setMsg("请输入全部的数据");
			responseInfo.setState(500);
			return responseInfo;
		} catch (Exception e) {
			System.out.println("其他错误");
			e.printStackTrace();

			ResponseInfo responseInfo = new ResponseInfo();
			responseInfo.setMsg("其他错误");
			responseInfo.setState(500);
			return responseInfo;
		}

	}

}
